package com.etxxy.web.security;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.etxxy.common.util.SerializableUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class RedisManager {

	private static final Logger logger = LoggerFactory.getLogger(RedisManager.class);

	private JedisPool jedisPool;

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	/**
	 * 获取redis中的key对应的值
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		Jedis jedis = null;
		String value = "";
		try {
			jedis = jedisPool.getResource();
			value = jedis.get(key);
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis.get异常: " + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis.get异常: " + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
		return value;
	}

	public Object getObject(byte[] data) {
		Jedis jedis = null;
		Object obj = null;
		try {
			jedis = jedisPool.getResource();
			byte[] result = jedis.get(data);
			if (result != null) {
				obj = SerializableUtil.deserialize(result);
			}
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis.get异常: " + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis.get异常: " + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
		return obj;
	}

	/**
	 * 删除一个指定的key
	 * 
	 * @param key
	 */
	public void remove(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(key);
			jedis.del(SerializableUtil.serialize(key));
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis.del异常: " + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis.del异常: " + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
	}

	public void remove(byte[] data) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(data);
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis.del异常: " + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis.del异常: " + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
	}

	/**
	 * 模糊查询返回符合条件的key的集合
	 * 
	 * @param pattern
	 * @return
	 */
	public Set<String> keys(String pattern) {
		Jedis jedis = null;

		try {
			jedis = jedisPool.getResource();
			return jedis.keys(pattern);
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis.keys异常: " + e.toString(), e);
			return null;
		} catch (Exception e) {
			logger.error("jedis.keys异常: " + e.toString(), e);
			return null;
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
	}

	/**
	 * @ca 清除所有
	 */
	public void flushDB(String pattern) {
		Set<String> keys = keys(pattern);
		if (null != keys && keys.size() > 0) {
			String[] array = keys.toArray(new String[] {});
			for (String key : array) {
				remove(key);
			}
		}
	}

	/**
	 * @category 存在的数量
	 * @param pattern
	 * @return
	 */
	public int dbSize(String pattern) {
		Set<String> keys = keys(pattern);
		if (null != keys) {
			return keys.size();
		}
		return 0;
	}

	/**
	 * 模糊查询返回符合条件的key的集合
	 * 
	 * @param pattern
	 * @return
	 */
	public Set<byte[]> byteKeys(String pattern) {
		Set<String> keys = keys(pattern);
		Set<byte[]> sets = new HashSet<byte[]>();
		if (null != keys && keys.size() > 0) {
			for (String key : keys) {
				sets.add(key.getBytes());
			}
		}
		return sets;
	}

	/**
	 * 设置一个具有生存周期的key-value 主要用于缓存
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 */
	public void setex(String key, Object value, int timeout) {
		Jedis jedis = null;

		try {
			jedis = jedisPool.getResource();
			jedis.setex(SerializableUtil.serialize(key), timeout, SerializableUtil.serialize(value));
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis setex异常：" + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis setex异常：" + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
	}

	/**
	 * 设置一个具有生存周期的key-value 主要用于缓存
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 */
	public void setex(byte[] key, byte[] value, int timeout) {
		Jedis jedis = null;

		try {
			jedis = jedisPool.getResource();
			jedis.setex(key, timeout, value);
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis setex异常：" + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis setex异常：" + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
	}

	/**
	 * 设置一个具有生存周期的key-value 主要用于缓存
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 */
	public void set(String key, String value) {
		Jedis jedis = null;

		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} catch (JedisConnectionException e) {
			if (null != jedis) {
				jedisPool.returnBrokenResource(jedis);
				jedis = null;
			}

			logger.error("jedis setex异常：" + e.toString(), e);
		} catch (Exception e) {
			logger.error("jedis setex异常：" + e.toString(), e);
		} finally {
			if (null != jedis) {
				jedisPool.returnResource(jedis);
			}
		}
	}

	/**
	 * @category 缓存时间
	 * @return
	 */
	public Integer getDefaultExpire() {
		return 30 * 60;
	}

}
